# -*- coding: utf-8 -*-

from pydantic import BaseModel, EmailStr

from fastapi_mail import FastMail, ConnectionConfig, MessageSchema, MessageType

from app.config import settings


emial_connection_config = ConnectionConfig(
    MAIL_USERNAME=settings.MAIL_USERNAME,
    MAIL_PASSWORD=settings.MAIL_PASSWORD,
    MAIL_FROM=settings.MAIL_FROM,
    MAIL_PORT=settings.MAIL_PORT,
    MAIL_SERVER=settings.MAIL_SERVER,
    MAIL_SSL_TLS=settings.MAIL_SSL_TLS,
    MAIL_STARTTLS =settings.MAIL_STARTTLS,  # 是否使用SSL: 587
    USE_CREDENTIALS=settings.USE_CREDENTIALS,  # 是否使用认证
    VALIDATE_CERTS=settings.VALIDATE_CERTS,  # 是否验证证书
)

class VerifyEmailCode(BaseModel):
    email: EmailStr
    code: str

class Email(BaseModel):
    addresses: list[EmailStr]
    subject: str
    content: str


async def send_message(email: Email):
    
    body = f"""
    <h1>{email.subject}</h1>
    <p>{email.content}</p>
    """

    message = MessageSchema(
        subject=email.subject,
        recipients=email.addresses,
        body=body,
        subtype=MessageType.html
    )

    fm = FastMail(emial_connection_config)

    await fm.send_message(message) 
